from agentDesign.L1.read_map import AStarPlanner, ReadMap
import pandas as pd
import numpy as np
import random

time_step = 1080
ONE_DAY = 360
ONE_HOUR = 15
ONE_MIN = 2
WALK_NUM = 100
read_map = ReadMap()
door_list_random = read_map.get_door("公司")
init_list = [(15, 15), (64, 50), (293, 73), (348, 73), (336, 119),
(26, 23), (310, 131), (298, 148), (357, 63), (416, 140)]
user_list_random = read_map.get_door("公司")
home_list = read_map.get_door("洋房")
print(home_list)
all_name = ["加油站", "体育场", "服装店", "公司", "鞋店", "饭店", "礼品店", "医院", "工厂", "百货大楼",
            "咖啡店", "音乐坊", "药房", "书店", "面包店", "水果店", "4s店", "教学楼", "学校食堂", "停车场", "洋房", "别墅",
            "公园", "高层", "游乐园", "消防站", "银行", "酒店", "商场", "超市", "警察局"]
random_list = []
for info in all_name:
    random_list += read_map.get_door(info)
print(random_list)
all_door_list = []
for name in all_name:
    all_door_list.append(read_map.get_door(name)[0])
# 划分区域
NUM_REGION = 10
Region_info = [[(0, 0), (0, 200), (18, 0), (18, 200)],  # 公司
               [(120, 0), (56, 0), (120, 200), (56, 200)],
               [(121, 66), (300, 66), (121, 107), (300, 107)],
               [(396, 66), (300, 66), (300, 107), (396, 107)],
               [(396, 107), (396, 200), (331, 200), (331, 107)],
               [(18, 0), (56, 0), (18, 200), (56, 200)],  # 饭店
               [(331, 107), (121, 107), (331, 139), (121, 139)],
               [(331, 139), (121, 139), (331, 200), (121, 200)],
               [(121, 0), (396, 0), (121, 66), (396, 66)],
               [(430, 0), (430, 200), (396, 0), (396, 200)]]  # 先公司，后饭店

Region_centre = [(9, 100), (88, 100), (161, 86), (384, 86), (348, 153), (37, 100), (226, 123), (226, 170), (259, 33), (413, 100)]
REGION_LINE = 212
REGION_WORK = 5

df_map = pd.read_csv('agentDesign/L1/map.csv', header=None)
map_matrix = df_map.values
# 对地图矩阵进行转置
map_matrix = map_matrix.transpose()


def point_in_rectangle(rectangle, point):
    # 矩形的四个顶点坐标
    x1, y1 = rectangle[0]
    x2, y2 = rectangle[1]
    x3, y3 = rectangle[2]
    x4, y4 = rectangle[3]
    x_min = min(x1, x2, x3, x4)
    x_max = max(x1, x2, x3, x4)
    y_min = min(y1, y2, y3, y4)
    y_max = max(y1, y2, y3, y4)
    # 检查点的 x 坐标是否在矩形的左右边界之间
    if int(x_min) <= int(point[0]) <= int(x_max):
        # 检查点的 y 坐标是否在矩形的上下边界之间
        if int(y_min) <= int(point[1]) <= int(y_max):
            return True

    return False


def get_which_region(position):
    # 获取属于的region
    node = position
    # 返回前往的区域编号
    for index, region in enumerate(Region_info):
        if point_in_rectangle(region, node):
            return index
    return random.randint(0, 9)


def mean_point(region_points):
    # 返回中心点
    region_np = np.array(region_points)
    center = np.mean(region_np, axis=0)
    return center


def points_dis(point1, point2):
    # 两点之间的距离
    dis = ((point1[0] - point2[0]) ** 2 +
           (point1[1] - point2[1]) ** 2) ** 0.5
    return dis

def distance_matrix():
    # 创建一个全零矩阵，用于存储距离信息
    dist_matrix = np.zeros((NUM_REGION, NUM_REGION))

    # 遍历每对区域的组合
    for i in range(NUM_REGION):
        for j in range(i + 1, NUM_REGION):
            # 选择每个区域的第一个点作为代表

            # 计算欧氏距离并赋值给距离矩阵的对应位置
            dist_matrix[i, j] = euclidean_distance(Region_info[i], Region_info[j])
            dist_matrix[j, i] = dist_matrix[i, j]

    return dist_matrix


def euclidean_distance(region1, region2):
    # 计算两点之间的距离
    point1 = mean_point(region1)
    point2 = mean_point(region2)
    return np.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)

